package priv.zgh.forum.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import priv.zgh.forum.auth.entity.Power;
import priv.zgh.forum.auth.entity.RolePower;
import priv.zgh.forum.auth.mapper.RolePowerMapper;
import priv.zgh.forum.auth.service.PowerService;
import priv.zgh.forum.auth.service.RolePowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import priv.zgh.forum.auth.service.RoleService;
import priv.zgh.forum.auth.vo.RolePowerVo;
import priv.zgh.forum.utils.R;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZGH
 * @since 2021-10-22
 */
@Service
public class RolePowerServiceImpl extends ServiceImpl<RolePowerMapper, RolePower> implements RolePowerService {
    RoleService roleService;
    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    PowerService powerService;
    @Autowired
    public void setPowerService(PowerService powerService) {
        this.powerService = powerService;
    }

    @Override
    public RolePowerVo getRolePowerById(String id) {
        // 查询角色信息
        RolePowerVo rolePowerVo = new RolePowerVo();
        rolePowerVo.setRole(roleService.getById(id));
        // 先查询角色所属权限id
        QueryWrapper<RolePower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        List<RolePower> rolePowers = baseMapper.selectList(queryWrapper);
        // 遍历角色所属权限id拼接sql语句查询结果
        if(rolePowers.size() > 0) {
            QueryWrapper<Power> powerQueryWrapper = new QueryWrapper<>();
            for (int i = 0; i < rolePowers.size(); i++) {
                if (i == 0) {
                    powerQueryWrapper.eq("id", rolePowers.get(i).getPowerId());
                    continue;
                }
                powerQueryWrapper.or().eq("id", rolePowers.get(i).getPowerId());
            }
            List<Power> powers = powerService.list(powerQueryWrapper);
            rolePowerVo.setPowers(powers);
        }else{
            rolePowerVo.setPowers(new ArrayList<>());
        }
        return rolePowerVo;
    }

    @Override
    public List<Power> getRoleNotPower(String id) {
        // 先查询属于角色的权限id
        QueryWrapper<RolePower> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",id);
        List<RolePower> rolePowers = baseMapper.selectList(queryWrapper);
        // 排除掉属于角色的权限
        QueryWrapper<Power> powerQueryWrapper = new QueryWrapper<>();
        for(int i = 0; i < rolePowers.size(); i++){
            String powerId = rolePowers.get(i).getPowerId();
            if(i == 0){
                powerQueryWrapper.ne("id",powerId);
                continue;
            }
            powerQueryWrapper.and(wrapper -> wrapper.ne("id",powerId));
        }
        List<Power> powers = powerService.list(powerQueryWrapper);
        return powers;
    }
}
